<template>
  <div>
    <pro-table
      ref="tableRef"
      :data="tableData"
      :total="total"
      :columns="columns"
      :tool-button="false"
      @search="commEvent.onSearch"
      :page-change="commEvent.onPageChange"
    >
    </pro-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, h } from "vue"
import ProTable from "@/components/ProTable/index.vue"
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface"
import { messagePage } from "@/api/modules/message"
import { SystemSpace } from "@/api/interface/message"
// 参数
const tableRef = ref<ProTableInstance>() // 表格实例
const tableData = ref<SystemSpace.MessagePage[]>([]) // 表格数据
const total = ref<number>(0) // 数据总数
// 页码参数
const pageParams = ref({
  type: 3,
  pageIndex: 1,
  pageSize: 10
})
// 表格列配置
const columns: ColumnProps<SystemSpace.MessagePage>[] = [
  {
    prop: "sort",
    label: "序号",
    width: 80,
    render: scope => h("span", scope.$index + 1 + (pageParams.value.pageIndex - 1) * pageParams.value.pageSize)
  },
  { prop: "title", label: "标题", search: { el: "input" } },
  { prop: "content", label: "内容" },
  { prop: "createTime", label: "创建时间" }
]
// 事件集合
const commEvent = {
  // 获取表格数据
  getTableData: async (params: SystemSpace.MessagePageQuery) => {
    const { data } = await messagePage(params)
    tableData.value = data.result
    total.value = data.totalCount
  },
  // 查询
  onSearch: () => {
    tableRef.value?.search()
    pageParams.value.pageIndex = 1
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  // 分页
  onPageChange: (page: number, size: number) => {
    pageParams.value.pageIndex = page
    pageParams.value.pageSize = size
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  //  新增/编辑
  onDetail: (row: SystemSpace.MessagePage) => {
    console.log(row)
  }
}
onMounted(() => {
  commEvent.getTableData(pageParams.value)
})
</script>

<style lang="scss" scoped></style>
